package com.usion.springbootmysql.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usion.springbootmysql.dao.LoginDao;
import com.usion.springbootmysql.dao.UserDao;
import com.usion.springbootmysql.pojo.ResultVo;
import com.usion.springbootmysql.pojo.TbUser;
import com.usion.springbootmysql.service.LoginService;
import com.usion.springbootmysql.utils.AliCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginDao loginDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultVo sendCode(String phone) throws Exception {
        //1.判断是否为一个正确手机号
        Boolean isPhone= Validator.isMobile(phone);
        //2.判断手机是否注册
        if (isPhone){
            //2.判断手机是否已经注册
            int count=loginDao.getUserCountByPhone(phone);
            if (count>0){
                //手机号存在
                //3.发送验证码
                AliCodeUtils aliCodeUtils=new AliCodeUtils();
                aliCodeUtils.sendLoginCode(phone,"123455");
                stringRedisTemplate.opsForValue().set(phone, "123455");
                stringRedisTemplate.expire(phone, 1, TimeUnit.MINUTES);
                return new ResultVo(200,"短信发送成功",null);
            }else {
                return new ResultVo(303,"此手机号不存在",null);
            }
        }else {
            return new ResultVo(303,"手机号格式错误",null);
        }
        //3。发送验证码
    }

    @Override
    public ResultVo userLogin(String phone, String code) {
        //1判断手机和验证码是否填写
       Boolean isHavephone= StrUtil.isEmpty(phone);
       Boolean isHaveCode = StringUtils.isEmpty(code);
       if (!isHavephone){
           if (!isHaveCode){
               //2用户判断是否已经存在
//               int count =loginDao.getUserCountByPhone(phone);
               LambdaQueryWrapper<TbUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
               lambdaQueryWrapper.eq(TbUser::getPhone, phone);
               TbUser tbUser=userDao.selectOne(lambdaQueryWrapper);
               if (tbUser!=null){
                   //3.验证验证码是否正确，匹配用户输入验证码和保存验证码是否一致
                    String redisCode =stringRedisTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCode)){
                        if (redisCode.equals(code)){
                            //4.返回用户个人信息
                            return new ResultVo(200,"success",tbUser);

                        }else {
                            return new ResultVo(303,"验证码输入错误",null);
                        }
                    }else {
                        return new ResultVo(303,"验证码已超期",null);
                    }

               }else {
                   return new ResultVo(303,"此手机号不存在或尚未激活",null);
               }
           }else {
               return new ResultVo(303,"验证码必填",null);
           }
       }else {
           return new ResultVo(303,"手机号必填",null);
       }
    }
}
